package com.example.content;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private Button button,button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);
        button = findViewById(R.id.button);
        button2 = findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri_user = Uri.parse("content://com.example.content.myprovider/user");
                // 获取ContentResolver
                ContentResolver resolver = getContentResolver();
                // 通过ContentResolver 向ContentProvider中查询数据
                Cursor cursor = resolver.query(uri_user, new String[]{"_id", "name"}, null, null, null);
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    String message = "query book: " + id + " " + name;
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                }
                cursor.close();
                // 关闭游标
                Uri uri_job = Uri.parse("content://com.example.content.myprovider/job");
                // 获取ContentResolver
                ContentResolver resolver2 = getContentResolver();
                // 通过ContentResolver 向ContentProvider中查询数据
                Cursor cursor2 = resolver2.query(uri_job, new String[]{"_id", "job"}, null, null, null);
                while (cursor2.moveToNext()) {
                    int id = cursor2.getInt(0);
                    String name = cursor2.getString(1);
                    String message = "query book: " + id + " " + name;
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                }
                cursor.close();
                // 关闭游标
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击返回
                Intent intent = new Intent(MainActivity.this,MyProvider_class.class);
                startActivity(intent);
            }
        });

    }
}